#include "StdAfx.h"
#include "MLabel.h"

namespace gui
{

MLabel::~MLabel(void)
{
}


/* Render */
void MLabel::Render( IRender* r )
{
    Vector3f bg = GetBackgroundColor();
    Vector3f fg = GetForegroundColor();


    // set background color
    r->SetColor( bg.GetX(), bg.GetY(), bg.GetZ(), GetAlpha() );

    Vector2f pos = GetLocation();
    r->FillRect( pos.x, pos.y, GetWidth(), GetHeight() );

    int charWidth = r->GetTextWidth( 'H' );
    int length = charWidth * m_labelText.length();

    std::string str;
    if ( length > GetWidth() )
    {
        int remaining = (GetWidth() - length) / charWidth;
        int npos = m_labelText.length() - remaining;
        str = m_labelText.substr( 0, npos );
    } 
    else
    {
        str = m_labelText;
    }
    r->SetColor( fg.GetX(), fg.GetY(), fg.GetZ() );
    r->DrawString( pos.x, pos.y, str.c_str() );

    // now draw all sub components
    gui::Component::Render( r );
}

};